<script setup>
import { ref } from 'vue'

// 问题类型
const categories = [
  { id: 1, name: '语音识别', icon: '🎙️' },
  { id: 2, name: '音质问题', icon: '🎵' },
  { id: 3, name: '设备连接', icon: '🔌' },
  { id: 4, name: '功能建议', icon: '💡' },
  { id: 5, name: '其他问题', icon: '❓' }
]

// 反馈表单
const feedbackForm = ref({
  category: '',
  title: '',
  description: '',
  images: [],
  contact: '',
  priority: 'normal'
})

// 反馈历史
const ticketHistory = ref([
  {
    id: 'T2024031501',
    category: '语音识别',
    title: '语音指令无法识别',
    status: 'pending',
    time: '2024-03-15 10:30',
    response: null
  },
  {
    id: 'T2024031502',
    category: '音质问题',
    title: '音质失真严重',
    status: 'resolved',
    time: '2024-03-15 14:20',
    response: {
      content: '已修复音频解码问题，请更新至最新版本',
      time: '2024-03-15 15:30'
    }
  }
])

// 提交反馈
const submitFeedback = () => {
  if (!feedbackForm.value.category || !feedbackForm.value.title || !feedbackForm.value.description) {
    alert('请填写必要信息')
    return
  }
  
  // 模拟提交
  const newTicket = {
    id: `T${Date.now()}`,
    category: feedbackForm.value.category,
    title: feedbackForm.value.title,
    status: 'pending',
    time: new Date().toLocaleString(),
    response: null
  }
  
  ticketHistory.value.unshift(newTicket)
  
  // 重置表单
  feedbackForm.value = {
    category: '',
    title: '',
    description: '',
    images: [],
    contact: '',
    priority: 'normal'
  }
  
  alert('反馈提交成功')
}

// 上传图片
const uploadImage = (event) => {
  const files = event.target.files
  if (files && files.length > 0) {
    // 限制上传数量
    if (feedbackForm.value.images.length + files.length > 3) {
      alert('最多上传3张图片')
      return
    }
    
    // 处理图片上传
    Array.from(files).forEach(file => {
      const reader = new FileReader()
      reader.onload = (e) => {
        feedbackForm.value.images.push(e.target.result)
      }
      reader.readAsDataURL(file)
    })
  }
}

// 删除图片
const removeImage = (index) => {
  feedbackForm.value.images.splice(index, 1)
}
</script>

<template>
  <div class="tickets-page">
    <!-- 提交反馈 -->
    <div class="submit-section">
      <h2>问题反馈</h2>
      <div class="feedback-form">
        <!-- 问题分类 -->
        <div class="form-group">
          <label>问题类型 <span class="required">*</span></label>
          <div class="category-options">
            <button
              v-for="category in categories"
              :key="category.id"
              class="category-btn"
              :class="{ active: feedbackForm.category === category.name }"
              @click="feedbackForm.category = category.name"
            >
              <span class="category-icon">{{ category.icon }}</span>
              <span class="category-name">{{ category.name }}</span>
            </button>
          </div>
        </div>

        <!-- 问题标题 -->
        <div class="form-group">
          <label>问题标题 <span class="required">*</span></label>
          <input
            v-model="feedbackForm.title"
            type="text"
            placeholder="请简要描述问题"
          >
        </div>

        <!-- 问题描述 -->
        <div class="form-group">
          <label>问题描述 <span class="required">*</span></label>
          <textarea
            v-model="feedbackForm.description"
            placeholder="请详细描述问题出现的场景和具体表现..."
            rows="4"
          ></textarea>
        </div>

        <!-- 图片上传 -->
        <div class="form-group">
          <label>相关截图</label>
          <div class="upload-area">
            <div class="image-list">
              <div
                v-for="(image, index) in feedbackForm.images"
                :key="index"
                class="image-item"
              >
                <img :src="image" alt="screenshot">
                <button 
                  class="remove-btn"
                  @click="removeImage(index)"
                >
                  ×
                </button>
              </div>
              <label 
                v-if="feedbackForm.images.length < 3"
                class="upload-btn"
              >
                <input
                  type="file"
                  accept="image/*"
                  multiple
                  @change="uploadImage"
                >
                <span class="upload-icon">+</span>
                <span class="upload-text">上传图片</span>
              </label>
            </div>
            <div class="upload-tip">
              支持jpg、png格式，最多3张
            </div>
          </div>
        </div>

        <!-- 联系方式 -->
        <div class="form-group">
          <label>联系方式</label>
          <input
            v-model="feedbackForm.contact"
            type="text"
            placeholder="请留下您的联系方式，方便我们及时反馈"
          >
        </div>

        <!-- 优先级 -->
        <div class="form-group">
          <label>优先级</label>
          <div class="priority-options">
            <label class="priority-option">
              <input
                v-model="feedbackForm.priority"
                type="radio"
                value="low"
              >
              <span class="priority-text">低</span>
            </label>
            <label class="priority-option">
              <input
                v-model="feedbackForm.priority"
                type="radio"
                value="normal"
              >
              <span class="priority-text">中</span>
            </label>
            <label class="priority-option">
              <input
                v-model="feedbackForm.priority"
                type="radio"
                value="high"
              >
              <span class="priority-text">高</span>
            </label>
          </div>
        </div>

        <button 
          class="submit-btn"
          @click="submitFeedback"
        >
          提交反馈
        </button>
      </div>
    </div>

    <!-- 反馈历史 -->
    <div class="history-section">
      <h2>反馈历史</h2>
      <div class="tickets-list">
        <div 
          v-for="ticket in ticketHistory"
          :key="ticket.id"
          class="ticket-card"
        >
          <div class="ticket-header">
            <div class="ticket-info">
              <div class="ticket-id">{{ ticket.id }}</div>
              <div 
                class="ticket-status"
                :class="ticket.status"
              >
                {{ 
                  ticket.status === 'pending' ? '处理中' : 
                  ticket.status === 'resolved' ? '已解决' : 
                  '已关闭'
                }}
              </div>
            </div>
            <div class="ticket-time">{{ ticket.time }}</div>
          </div>
          <div class="ticket-content">
            <div class="ticket-category">{{ ticket.category }}</div>
            <div class="ticket-title">{{ ticket.title }}</div>
          </div>
          <div 
            v-if="ticket.response"
            class="ticket-response"
          >
            <div class="response-content">
              {{ ticket.response.content }}
            </div>
            <div class="response-time">
              {{ ticket.response.time }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.tickets-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 3rem;
}

/* 提交反馈样式 */
.submit-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.submit-section h2 {
  margin: 0;
  margin-bottom: 2rem;
}

.feedback-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.form-group label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.required {
  color: var(--error-color);
}

/* 问题分类样式 */
.category-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1rem;
}

.category-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: var(--text-gray);
  cursor: pointer;
  transition: all 0.3s ease;
}

.category-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.category-btn.active {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  color: white;
}

.category-icon {
  font-size: 1.5rem;
}

/* 输入框样式 */
input,
textarea {
  padding: 0.8rem;
  background-color: var(--bg-dark);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: var(--text-light);
  resize: vertical;
}

/* 图片上传样式 */
.upload-area {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.image-list {
  display: flex;
  gap: 1rem;
}

.image-item {
  width: 100px;
  height: 100px;
  position: relative;
}

.image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  background-color: var(--error-color);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
}

.upload-btn {
  width: 100px;
  height: 100px;
  background-color: var(--bg-dark);
  border: 2px dashed rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.upload-btn:hover {
  border-color: var(--primary-color);
}

.upload-btn input {
  display: none;
}

.upload-icon {
  font-size: 1.5rem;
  color: var(--text-gray);
}

.upload-text {
  font-size: 0.9rem;
  color: var(--text-gray);
}

.upload-tip {
  color: var(--text-gray);
  font-size: 0.9rem;
}

/* 优先级选项样式 */
.priority-options {
  display: flex;
  gap: 1.5rem;
}

.priority-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.priority-option input {
  display: none;
}

.priority-text {
  padding: 0.5rem 1.5rem;
  background-color: var(--bg-dark);
  border-radius: 20px;
  color: var(--text-gray);
  transition: all 0.3s ease;
}

.priority-option input:checked + .priority-text {
  background-color: var(--primary-color);
  color: white;
}

/* 提交按钮样式 */
.submit-btn {
  padding: 1rem;
  background-color: var(--primary-color);
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background-color: var(--secondary-color);
}

/* 反馈历史样式 */
.history-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.history-section h2 {
  margin: 0;
  margin-bottom: 2rem;
}

.tickets-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.ticket-card {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 1.5rem;
}

.ticket-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.ticket-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.ticket-id {
  color: var(--text-gray);
  font-family: monospace;
}

.ticket-status {
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  font-size: 0.9rem;
}

.ticket-status.pending {
  background-color: var(--warning-color);
  color: black;
}

.ticket-status.resolved {
  background-color: var(--success-color);
  color: white;
}

.ticket-time {
  color: var(--text-gray);
  font-size: 0.9rem;
}

.ticket-content {
  margin-bottom: 1rem;
}

.ticket-category {
  color: var(--text-gray);
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.ticket-title {
  font-weight: 500;
}

.ticket-response {
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 1rem;
}

.response-content {
  color: var(--success-color);
  margin-bottom: 0.5rem;
}

.response-time {
  color: var(--text-gray);
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .category-options {
    grid-template-columns: repeat(2, 1fr);
  }

  .priority-options {
    flex-direction: column;
    gap: 0.8rem;
  }

  .priority-option {
    width: 100%;
  }

  .priority-text {
    flex: 1;
    text-align: center;
  }
}
</style>